package com.springBoot.springBootSysCore.common.service.jpaQuery;

import com.springBoot.springBootSysCore.common.StaticConfig;
import com.springBoot.springBootSysCore.common.persistence.DataTables;
import com.springBoot.springBootSysCore.common.persistence.LayuiTables;
import com.springBoot.springBootSysCore.common.service.IBaseService;
import com.springBoot.springBootSysCore.common.utils.DateUtils;
import com.springBoot.springBootSysCore.common.utils.returnUtil.CodeEnum;
import com.springBoot.springBootSysCore.common.utils.returnUtil.PageUtils;
import com.springBoot.springBootSysCore.common.utils.returnUtil.ResultJsonFormat;
import com.springBoot.springBootSysCore.modules.entity.utils.DictUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Page;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import javax.servlet.http.HttpServletRequest;
import java.beans.PropertyEditorSupport;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.*;

public class IBaseController<T, PK extends Serializable> {

	@Autowired
	protected IBaseService<T, PK> iBaseService;
	
	
	//分页查询--多对多查询--search_EQ_@sysOrganizationClass#organizationClass#id  必须要在sysOrganizationClass对象里加入注解@Component("sysOrganizationClass")
	/**
	 * 既定分页查询--将返回数据封装
	 * 默认已集成 EQ, NOTEQ, LIKE, GT, LT, GTE, LTE, NULL;
	 * 属性名写法：search_EQ_name  search_LIKE_sysUser.id
	 * @param pageNo 当前页
	 * @param pageSize 每页条数
	 * @param model 
	 * @param request
	 * @return
	 */
	public void queryByPage(int pageNo, int pageSize, Model model, HttpServletRequest request, String sortName, String sortType){
		queryByPage(pageNo, pageSize, model, request, null, sortName, sortType);
	}
	/**
	 * 既定分页查询--可自定义加入参数--将返回数据封装
	 * 默认已集成 EQ, NOTEQ, LIKE, GT, LT, GTE, LTE, NULL;
	 * 属性名写法：search_EQ_name  search_LIKE_sysUser.id
	 * @param pageNo 当前页
	 * @param pageSize 每页条数
	 * @param model 
	 * @param request
	 * @return
	 */
	public void queryByPage(int pageNo, int pageSize, Model model, HttpServletRequest request, Map<String, Object> param, String sortName, String sortType){
		Map<String, Object> params = IWebUtils.getParametersStartingWith(request, StaticConfig.SEARCH_KEY);//获取参数名及值
		if(param!=null){
			params.putAll(param);
		}
		Page<T> page = iBaseService.getPage(params, PageUtils.buildPageRequest(pageNo, pageSize, sortName, sortType));
		ResultJsonFormat rj = new ResultJsonFormat("查询成功", page.getContent(), page.hasNext(), pageNo,pageSize,page.getTotalElements(), CodeEnum.SUCCESS.value());
		model.addAttribute("data", rj.convertResultJsonForData());//数据
		String queryString = IWebUtils.encodeParameterStringWithPrefix(params, StaticConfig.SEARCH_KEY);
		model.addAttribute("searchParams", queryString);//在页面放置查询条件
	}
	/**
	 * 既定分页查询--将返回数据封装--json
	 * 默认已集成 EQ, NOTEQ, LIKE, GT, LT, GTE, LTE, NULL;
	 * 属性名写法：search_EQ_name  search_LIKE_sysUser.id
	 * @param pageNo 当前页
	 * @param pageSize 每页条数
	 * @param model 
	 * @param request
	 * @return
	 */
	public Map<String, Object> queryByPageForJson(int pageNo, int pageSize, Model model, HttpServletRequest request, String sortName, String sortType){
		return queryByPageForJson(pageNo, pageSize, model, request, null, sortName, sortType);
	}
	/**
	 * 既定分页查询--可自定义加入参数--将返回数据封装-json
	 * 默认已集成 EQ, NOTEQ, LIKE, GT, LT, GTE, LTE, NULL;
	 * 属性名写法：search_EQ_name  search_LIKE_sysUser.id
	 * @param pageNo 当前页
	 * @param pageSize 每页条数
	 * @param model 
	 * @param request
	 * @return
	 */
	public Map<String, Object> queryByPageForJson(int pageNo, int pageSize, Model model, HttpServletRequest request, Map<String, Object> param, String sortName, String sortType){
		Map<String, Object> params = IWebUtils.getParametersStartingWith(request, StaticConfig.SEARCH_KEY);//获取参数名及值
		if(param!=null){
			params.putAll(param);
		}
		Page<T> page = iBaseService.getPage(params,PageUtils.buildPageRequest(pageNo, pageSize, sortName, sortType));
		ResultJsonFormat rj = new ResultJsonFormat("查询成功", page.getContent(), page.hasNext(), pageNo,pageSize,page.getTotalElements(), CodeEnum.SUCCESS.value());
		return  rj.convertResultJsonForData();
	}

	public Map<String, Object> findAllForJson(Model model, HttpServletRequest request, Map<String, Object> param, String sortName, String sortType){
		Map<String, Object> params = IWebUtils.getParametersStartingWith(request, StaticConfig.SEARCH_KEY);//获取参数名及值
		if(param!=null){
			params.putAll(param);
		}
		ResultJsonFormat rj = new ResultJsonFormat("查询成功",iBaseService.findAll(param),CodeEnum.SUCCESS.value());
		return  rj.convertResultJsonForData();
	}

	/**
	 * 分页查询 返回datatables
	 * @param model
	 * @param request
	 * @param param
	 * @param sortName
	 * @param sortType
	 * @return
	 */
	public DataTables queryByPageForDataTables( Model model, HttpServletRequest request, Map<String, Object> param, String sortName, String sortType){
		Map<String, Object> params = IWebUtils.getParametersStartingWith(request, StaticConfig.SEARCH_KEY);//获取参数名及值
		if(param!=null){
			params.putAll(param);
		}
		DataTables dt = DataTables.getInstance(request, null);
		Page<T> page = iBaseService.getPage(params,PageUtils.buildPageRequest(dt.getStart()/dt.getLength() + 1, dt.getLength(), sortName, sortType));
		dt.setData(page.getContent());
		dt.setRecordsFiltered(page.getTotalElements());
		dt.setOtherData(param);
		return dt;
	}

	/**
	 * 分页查询 返回layui 数据格式
	 * @param model
	 * @param request
	 * @param param
	 * @param sortName
	 * @param sortType
	 * @return
	 */
	public LayuiTables queryByPageForLayuiTables(Model model, HttpServletRequest request, Map<String, Object> param, String sortName, String sortType){
		Map<String, Object> params = IWebUtils.getParametersStartingWith(request, StaticConfig.SEARCH_KEY);//获取参数名及值
		if(param!=null){
			params.putAll(param);
		}
		int pageNo = Integer.valueOf(request.getParameter("page"));
		int pageSize = Integer.valueOf(request.getParameter("limit"));
		Page<T> page = iBaseService.getPage(params,PageUtils.buildPageRequest(pageNo/pageSize + 1, pageSize, sortName, sortType));
		return new LayuiTables(page.getTotalElements(),page.getContent());
	}

	/**
	 * 既定分页查询
	 * 默认已集成 EQ, NOTEQ, LIKE, GT, LT, GTE, LTE, NULL;
	 * 属性名写法：search_EQ_name  search_LIKE_sysUser.id
	 * @param pageNo 当前页
	 * @param pageSize 每页条数
	 * @param model 
	 * @param request
	 * @return
	 */
	public void list(int pageNo, int pageSize, Model model, HttpServletRequest request, String sortName, String sortType){
		list(pageNo, pageSize, model, request, null, sortName, sortType);
	}
	/**
	 * 既定分页查询--可自定义加入参数
	 * 默认已集成 EQ, NOTEQ, LIKE, GT, LT, GTE, LTE, NULL;
	 * 属性名写法：search_EQ_name  search_LIKE_sysUser.id
	 * @param pageNo 当前页
	 * @param pageSize 每页条数
	 * @param model 
	 * @param request
	 * @return
	 */
	public void list(int pageNo, int pageSize, Model model, HttpServletRequest request, Map<String, Object> param, String sortName, String sortType){
		Map<String, Object> params = IWebUtils.getParametersStartingWith(request, StaticConfig.SEARCH_KEY);//获取参数名及值
		if(param!=null){
			params.putAll(param);
		}
		Page<T> page = iBaseService.getPage(params,PageUtils.buildPageRequest(pageNo, pageSize, sortName, sortType));
		model.addAttribute("data", page);//数据
		String queryString = IWebUtils.encodeParameterStringWithPrefix(params, StaticConfig.SEARCH_KEY);
		model.addAttribute("searchParams", queryString);//在页面放置查询条件
	}
	
	/*ListJoin<SysUser, SysOrganization> orgIdP= root.join(root.getModel().getList("orgList",SysOrganization.class) , JoinType.LEFT);
	List<String> orgli = sysOrganizationRepositories.getOrgIdByUser(logUser.getId());
	orgli.add("");//处理没有机构的情况
	list.add(cb.or(orgIdP.get("id").as(String.class).in(orgli),orgIdP.get("id").as(String.class).isNull()));*/
	//多对多写法--暂未写入


}
